﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace AdvanceLib.Core.Configuration
{
    [XmlRoot("Dictionary")]
    public class SerializableDictionary<TKey, TValue>
    {
        #region Constructors
        public SerializableDictionary()
        {
            this._keys = new List<TKey>();
            this._values = new List<TValue>();
        }
        #endregion

        #region Fields
        private List<TKey> _keys;
        private List<TValue> _values;
        #endregion

        #region Properties
        public List<TKey> Keys
        {
            get { return _keys; }
            set { _keys = value; }
        }
        public List<TValue> Values
        {
            get { return _values; }
            set { _values = value; }
        }
        #endregion

        #region Methods
        public static SerializableDictionary<TKey, TValue> FromDictionary(Dictionary<TKey, TValue> dict)
        {
            SerializableDictionary<TKey, TValue> wrapper = new SerializableDictionary<TKey, TValue>();
            foreach (KeyValuePair<TKey, TValue> pair in dict)
            {
                wrapper.Add(pair.Key, pair.Value);
            }
            return wrapper;
        }
        public Dictionary<TKey, TValue> ToDictionary()
        {
            Dictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
            for (int i = 0; i < this._keys.Count; i++)
            {
                dict.Add(this._keys[i], this._values[i]);
            }
            return dict;
        }
        public void Add(TKey key, TValue value)
        {
            this._keys.Add(key);
            this._values.Add(value);
        }
        #endregion
    }
}
